package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;

import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/admin/dish")
@Api(tags = "菜品管理")
public class DishController {

    @Autowired
    DishService dishService;

    @ApiOperation("新增菜品")
    @PostMapping
    @CacheEvict(cacheNames = "dishCache",key = "#dishDTO.categoryId")
    public Result addDish(@RequestBody DishDTO dishDTO) {
        log.info("新增菜品:{} ", dishDTO);
        dishService.addDish(dishDTO);
        return Result.success();
    }


    //菜品的分页查询
    @ApiOperation("菜品的分页查询")
    @GetMapping("/page")
    public Result<PageResult> pageDish(DishPageQueryDTO dishPageQueryDTO) {
        log.info("菜品的分页查询: {}", dishPageQueryDTO);
        PageResult pageResult = dishService.pageDish(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    //删除菜品
    @ApiOperation("删除菜品")
    @DeleteMapping
    @CacheEvict(cacheNames = "dishCache",allEntries = true)
    public Result deleteDish(@RequestParam List<Long> ids) {
        dishService.deleteDish(ids);
        return Result.success();
    }

    //根据id查询菜品
    @ApiOperation("根据id查询菜品")
    @GetMapping("/{id}")
    public Result<DishVO> getDishId(@PathVariable Long id) {
        DishVO dishVo = dishService.getDishId(id);
        return Result.success(dishVo);
    }

    //修改菜品
    @ApiOperation("修改菜品")
    @PutMapping
    @CacheEvict(cacheNames = "dishCache",key = "#dishDTO.categoryId")
    public Result updateDish(@RequestBody DishDTO dishDTO) {
        dishService.updateDish(dishDTO);
        return Result.success();
    }

    @ApiOperation("按分类id查询菜品")
    @GetMapping("/list")
    public Result<List<Dish>> getCategoryIdByDish(Long categoryId) {
        List<Dish> c = dishService.getCategoryIdByDish(categoryId);
        return Result.success(c);
    }

    @ApiOperation("菜品的起售/停售")
    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "dishCache",allEntries = true)
    public Result getstatus(@PathVariable Integer status, Long id) {
        dishService.getStatus(status, id);
        return Result.success();
    }
}
